<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/25
 * Time: 15:48
 */

namespace app\admin\model;


use think\Model;
use think\Validate;

class ConfigModel extends Model
{

    protected $table = 'yui_config';

    /*
     * rule
     * 验证data
     * @params $data
     * 新增和修改时验证
     * */
    public function validate_model()
    {
        $rule = [
            'mail_address' => 'require|email',
            'mail_loginname' => 'require|email',
            'mail_port' => 'require|number',
            'mail_password' => 'require',
            'mail_formname' => 'require|chsAlphaNum',
            'mail_smtp' => 'require|activeUrl'
        ];
        $msg = [
            'mail_address.require' => '邮箱地址不能为空!',
            'mail_address.email' => '邮箱地址不合法!',
            'mail_loginname.require' => '邮箱登录帐号不能为空!',
            'mail_loginname.email' => '邮箱登录帐号不合法!',
            'mail_port.require' => 'SMTP服务器端口不能为空!',
            'mail_port.number' => 'SMTP服务器端口不合法!',
            'mail_password.require' => '邮箱密码不能为空!',
            'mail_formname.require' => '发件人不能为空!',
            'mail_formname.chsAlphaNum' => '发件人不能包含特殊字符!',
            'mail_smtp.require' => '邮箱SMTP服务器不能为空!',
            'mail_smtp.activeUrl' => '邮箱SMTP服务器不合法!',
        ];
        $data = input('post.');
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            return [
                'code' => 0,
                'text' => $validate->getError()
            ];
        }
        return ['code' => 1];
    }

    /*
   * 列表
   * */
    public function listAll()
    {
        return $this->find();
    }

    /*
     * 开启
     * */
    public function openList(){
        return $this->where('open_mail=1')->find();
    }

    public function edit(){
        $data = input('post.');
        unset($data['__token__']);
        $id = input('post.id/d');
        if ($this->where('id', $id)->update($data)) {
            tag('logs_db', ['修改', '邮件配置', '更新配置']);
            return op_success();
        }
        return op_fail();
    }

}